﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.Technology
{
    public partial class uc_Locomotive_MainBase_WorkFlow_TrainWorkDay : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID, _LoadMWID, _LoadMDID, _LoadType, _LoadName, _LoadOrder, _LoadLength = "";

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bt_map_Click(object sender, EventArgs e)
        {
            openFileDialog_Map.Filter = "图片文件|*.jpg|图片文件|*.png";
            if (openFileDialog_Map.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog_Map.FileName != "")
                {
                    FileInfo _fi = new FileInfo(openFileDialog_Map.FileName);
                    if (_fi.Length / 1024 > 220)
                    {
                        openFileDialog_Map.FileName = "";
                        MessageBox.Show("图片应小于220KB,请重新选择！");
                    }
                    else
                    {
                        pb_Photo.ImageLocation = openFileDialog_Map.FileName;
                    }
                }
            }
        }

        private void cb_Type_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_Type.Checked)
            {
                cb_Type.Text = "卡控作业点";
            }
            else
            {
                cb_Type.Text = "非卡控作业点";
            }
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string f_type = "0";
            if (cb_Type.Checked)
            {
                f_type = "1";
            }
            if (_LoadType == "Add")
            {
                strSQL = "insert into TS_TR_PS_Work_Main_Day (f_mwid,f_ndid,f_name,f_length,f_order,f_type,f_workerid) values(" + _LoadMWID + "," + cb_ND.SelectedValue.ToString() + ",'" + tb_Name.Text.Trim() + "'," + tb_Length.Text.Trim() + "," + tb_Order.Text.Trim() + ","+f_type+"," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                _LoadID=TS_Win_Class.MySqlComGetID(strSQL);
            }
            if (_LoadType == "Edit")
            {
                strSQL = "update TS_TR_PS_Work_Main_Day set f_ndid=" + cb_ND.SelectedValue.ToString() + ",f_name='" + tb_Name.Text.Trim() + "',f_length=" + tb_Length.Text.Trim() + ",f_order=" + tb_Order.Text.Trim() + ",f_type="+f_type+",f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _LoadID;
                TS_Win_Class.MySqlCom(strSQL);
            }
            if (_LoadType == "Del")
            {
                strSQL = "update TS_TR_PS_Work_Main_Day set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _LoadID;
                TS_Win_Class.MySqlCom(strSQL);
            }
            try
            {
                if (openFileDialog_Map.FileName != "")
                {
                    string sql = "";

                    FileStream fs = new FileStream(openFileDialog_Map.FileName, FileMode.Open, FileAccess.Read);
                    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                    byte[] bt = br.ReadBytes(Convert.ToInt32(fs.Length));
                    string base64String = Convert.ToBase64String(bt);
                    br.Close();
                    fs.Close();
                    if (TS_Win_Class.GetFilledDataTable("select * from TS_S_B_Digital_Map_WorkState where  where f_del=0 and f_functype=1 and f_funcid=" + _LoadID).Rows.Count == 0)
                    {
                        sql = "insert into TS_S_B_Digital_Map_WorkState (f_funcid,f_functype,f_brief,f_size) values(" + _LoadID + ",1,'" + base64String + "','128,246')";
                    }
                    else
                    {
                        sql = "update TS_S_B_Digital_Map_WorkState set f_brief='" + base64String + "' where f_del=0 and f_functype=1 and f_funcid=" + _LoadID;
                    }
                    TS_Win_Class.MySqlCom(sql);
                }
            }
            catch
            {

            }

            this.DialogResult = DialogResult.OK;
        }

        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            _SetButtonEnabled();
        }
        private void BindPhoto()
        {
            try
            {
                pb_Photo.Image = null;
                DataTable dt = TS_Win_Class.GetFilledDataTable("select f_brief from TS_S_B_Digital_Map_WorkState where f_del=0 and f_functype=1 and f_funcid=" + _LoadID);
                if (dt.Rows.Count > 0)
                {
                    MemoryStream ms = new MemoryStream(Convert.FromBase64String(dt.Rows[0]["f_brief"].ToString()));
                    Image image = Image.FromStream(ms, true);
                    pb_Photo.Image = image;
                }
            }
            catch
            {

            }

        }

        private void uc_Locomotive_MainBase_WorkFlow_TrainWorkDay_Load(object sender, EventArgs e)
        {
            TS_Win_Class.FillListTypeAct(cb_ND, "select f_id,f_name from TS_S_T_S_RP_Name_Dictionary where f_del=0");
            if (_LoadType == "Add")
            {
                bt_Sure.Text = "确定新增";
            }
            else
            {
                if (_LoadType == "Edit")
                {
                    bt_Sure.Text = "确定修改";
                }
                else
                {
                    bt_Sure.Text = "确定删除";
                }
                string[] _str = TS_Win_Class.GetSQlFieldValue("select * from TS_TR_PS_Work_Main_Day where f_id="+_LoadID, new string[] {"f_ndid" ,"f_length","f_name","f_order","f_type"});
                cb_ND.SelectedValue = _str[0];
                tb_Length.Text = _str[1];
                tb_Name.Text = _str[2];
                tb_Order.Text = _str[3];
                if (_str[4] == "0")
                {
                    cb_Type.Checked = false;
                }
                else
                {
                    cb_Type.Checked = true;
                }
            }
            BindPhoto();
        }
        private void _SetButtonEnabled()
        {
            if (cb_ND.SelectedIndex > -1 && tb_Order.Text.Trim().Length > 0 && tb_Name.Text.Trim().Length > 0 && tb_Length.Text.Trim().Length > 0)
            {
                bt_Sure.Enabled = true;
            }
            else
            {
                bt_Sure.Enabled = false;
            }
        }
        public uc_Locomotive_MainBase_WorkFlow_TrainWorkDay()
        {
            InitializeComponent();
        }
    }
}